﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Enterprise.Framework.Frame.Architecture;
using Resto.CostControl.Core.Query.DishDeliveryModule.ViewObject;
using Enterprise.Framework.DomainModel.Specifications;
using Enterprise.Framework.DomainModel;
using Enterprise.Framework.DomainModel.Repositories;
using Resto.CostControl.Core.Domain.Model;

namespace Resto.CostControl.Core.Query.DishDeliveryModule.QueryObject
{
    public class DishDeliveryQO : IApplicationQueryObject
    {
        private IQueryable<DishDeliveryVO> GetDishDeliveryVOHistorySet()
        {
            IEntityRepository<DishDelivery> repository = RestoCostDomainSession.CurrentSession.CreateEntityRepository<DishDelivery>();

            IQueryable<DishDeliveryVO> list = from o in repository.Objects                                            
                                              select new DishDeliveryVO()
                                              {
                                                  DeliveryID = o.ID,
                                                  Address = o.Address,
                                                  Code = o.Code,
                                                  Date = o.Date,
                                                  StateCode = o.State,
                                                  SiteID = o.SiteID,
                                              };

            return list;
        }

        private IQueryable<DishDeliveryVO> GetDishDeliveryVOFutureSet()
        {
            IViewRepository<View_DishDeliveryByDay> repository = RestoCostViewSession.CurrentSession.CreateViewRepository<View_DishDeliveryByDay>();

            IQueryable<DishDeliveryVO> list = from o in repository.Objects
                                              select new DishDeliveryVO()
                                              {
                                                  DeliveryID = o.DeliveryID,
                                                  Address = o.Address,
                                                  Code = o.Code,
                                                  Date = o.Date,
                                                  StateCode = o.State,
                                                  SiteID = o.SiteID,
                                              };

            return list;
        }

        /// <summary>
        /// 查询历史订单
        /// </summary>
        /// <param name="spec"></param>
        /// <param name="pager"></param>
        /// <param name="orderKey"></param>
        /// <param name="isAscending"></param>
        /// <returns></returns>
        public DishDeliveryVO[] QueryDishDeliveryVOHistoryByPager(ISpecification<DishDeliveryVO> spec, PagerCondition pager, string orderKey, bool isAscending)
        {
            IQueryable<DishDeliveryVO> list = GetDishDeliveryVOHistorySet();

            if (spec != null)
                list = list.Where(spec.GetExpression());

            IEnumerable<DishDeliveryVO> list2 = RepositoryExtention.Query(list, pager, orderKey, isAscending);
            return list2.ToArray();
        }

        public DishDeliveryVO[] QueryDishDeliveryVOFutureByPager(ISpecification<DishDeliveryVO> spec, PagerCondition pager, string orderKey, bool isAscending)
        {
            IQueryable<DishDeliveryVO> list = GetDishDeliveryVOFutureSet();

            if (spec != null)
                list = list.Where(spec.GetExpression());

            IEnumerable<DishDeliveryVO> list2 = RepositoryExtention.Query(list, pager, orderKey, isAscending);
            return list2.ToArray();
        }
    }
}
